from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class SeckillPage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.seckill_item_locator = (By.ID, "seckill-item")
        self.start_time_locator = (By.ID, "start-time")
        self.end_time_locator = (By.ID, "end-time")
        self.remaining_time_locator = (By.ID, "remaining-time")
        self.join_seckill_button_locator = (By.ID, "join-seckill")
        self.seckill_status_locator = (By.ID, "seckill-status")
        self.seckill_result_locator = (By.ID, "seckill-result")
        self.seckill_items_list_locator = (By.ID, "seckill-items-list")
        self.seckill_countdown_locator = (By.ID, "countdown-timer")
        self.seckill_error_message_locator = (By.ID, "error-message")

    def navigate(self, seckill_id):
        self.driver.get(f'http://example.com/seckill/{seckill_id}')

    def get_seckill_item(self):
        item = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.seckill_item_locator)
        )
        return item.text

    def get_start_time(self):
        start_time = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.start_time_locator)
        )
        return start_time.text

    def get_end_time(self):
        end_time = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.end_time_locator)
        )
        return end_time.text

    def get_remaining_time(self):
        remaining_time = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.remaining_time_locator)
        )
        return remaining_time.text

    def join_seckill(self):
        join_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.join_seckill_button_locator)
        )
        join_button.click()

    def get_seckill_status(self):
        status = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.seckill_status_locator)
        )
        return status.text

    def get_seckill_result(self):
        result = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.seckill_result_locator)
        )
        return result.text

    def get_seckill_items_list(self):
        items = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.seckill_items_list_locator)
        )
        return [item.text for item in items]

    def get_seckill_countdown(self):
        countdown = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.seckill_countdown_locator)
        )
        return countdown.text

    def get_seckill_error_message(self):
        error_message = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.seckill_error_message_locator)
        )
        return error_message.text